home *** CD-ROM | disk | FTP | other *** search
- // ----------------------------------------------------------------------------------
- // File : demoCDEF.c
- // Purpose : Demo routine for various CDEF's and dialog routines
- // Author : Jim Stout
- // Date : November 2, 1991
- // ------------------------------------------------------------------------------------
- #include "TogLib.h"
- #include "movableModal.h"
- #include "demoCDEF.h"
-
- static void setNewFont (DialogPtr theDialog);
- static void setNewSize (DialogPtr theDialog, long newSize);
- static void doDialog (void);
- static pascal char filter (DialogPtr theDialog, EventRecord *theEvent, short *theItem);
- extern void updateWindow(WindowPtr);
-
- #define MAXFONTSIZES 5
-
- // ------------------------------------------------------------------------------------
- // routine called by movableModalDialog
- // ------------------------------------------------------------------------------------
- extern void updateWindow(WindowPtr w)
- {
- SetPort(w);
- BeginUpdate(w);
- EndUpdate(w);
- }
-
- // ------------------------------------------------------------------------------------
- // Mainline starts here :)
- // ------------------------------------------------------------------------------------
- main()
- {
- InitGraf(&thePort);
- InitWindows();
- InitFonts();
- InitMenus();
- TEInit();
- InitCursor();
- InitDialogs(0L);
-
- doDialog();
- }
-
- // ------------------------------------------------------------------------------------
- // lots of neat stuff follows…
- // ------------------------------------------------------------------------------------
- static void doDialog()
- {
-
- DialogPtr theDialog;
- short itemHit,t,item,newSize;
- long longVal;
- Rect r;
- ControlHandle h,h2;
- Str255 fontSize,s;
- popUpPrivateDataH hPpd;
- FontInfo fInfo;
-
- theDialog = GetNewDialog(128,0L,(DialogPtr)-1);
- if(theDialog) {
-
- SetPort(theDialog);
-
- initTogButtons(theDialog,TB1,TB4); // initialize TOG button stuff
-
- GetDItem(theDialog,SPIN2,&t,(Handle *)&h,&r); // set SPIN2 to update EDITSPIN
- longVal = MAKELONG(EDITSPIN, 10); // and to increment by 10's
- SetCRefCon(h,longVal);
- SelIText(theDialog, EDITSPIN, 0, 32767);
-
- GetDItem(theDialog,FONTPOPUP,&t,(Handle *)&h,&r); // set ctl value for popups
- hPpd = (popUpPrivateDataH)(*h)->contrlData; // and set the checkmarks
- SetCtlValue(h, 1);
- CheckItem((*hPpd)->mHandle, 1, checkMark);
-
- GetDItem(theDialog,FONTSIZE,&t,(Handle *)&h,&r);
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
- SetCtlValue(h, 3);
- CheckItem((*hPpd)->mHandle, 3, checkMark);
-
- ShowWindow(theDialog);
-
- while(itemHit != OK)
- {
- movableModalDialog((ProcPtr)filter,&itemHit); // do a movable dialog!!
-
- switch (itemHit) {
- case RB1:
- case RB2:
- case RB3:
- setRadioButtons(theDialog,itemHit,RB1,RB3);
- resetTogButtons(theDialog,TB1);
- break;
- case TB1:
- case TB2:
- case TB3:
- case TB4:
- setTogButtons(theDialog,itemHit,TB1,TB4);
- break;
- case CB1:
- case CB2:
- GetDItem(theDialog,itemHit, &t,(Handle *)&h,&r);
- SetCtlValue(h, !GetCtlValue(h));
- resetTogButtons(theDialog,TB1);
- break;
- case EDITSIZE:
- GetDItem(theDialog,EDITSIZE, &t,(Handle *)&h,&r);
- GetIText((Handle)h, s);
- StringToNum(s, &longVal);
- if(longVal > 128 || longVal < 0) {
- SetIText((Handle)h, "\p12");
- SysBeep(1);
- }
- else
- setNewSize(theDialog, longVal);
- break;
- case EDITSPIN:
- GetDItem(theDialog,EDITSPIN, &t,(Handle *)&h,&r);
- GetIText((Handle)h, s);
- StringToNum(s, &longVal);
- GetDItem(theDialog,SPIN2, &t,(Handle *)&h,&r);
- SetCtlValue(h, (short)longVal);
- break;
- case ENABLE:
- GetDItem(theDialog,CB1, &t,(Handle *)&h,&r);
- GetDItem(theDialog,ENABLE,&t,(Handle *)&h2,&r);
- if((*h)->contrlHilite == 0)
- SetCTitle(h2,"\pEnable\rControls");
- else
- SetCTitle(h2,"\pDisable\rControls");
-
- for(item=CB1;item<=SETBTN;item++) {
- GetDItem(theDialog,item,&t,(Handle *)&h,&r);
- if((*h)->contrlHilite == 0)
- HiliteControl(h,255);
- else
- HiliteControl(h,0);
- }
- resetTogButtons(theDialog,TB1);
- break;
- case FONTPOPUP:
- resetTogButtons(theDialog,TB1);
- break;
- case FONTSIZE:
- GetDItem(theDialog,FONTSIZE,&t,(Handle *)&h,&r);
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
-
- newSize = GetCtlValue(h);
- GetItem((*hPpd)->mHandle, newSize, fontSize);
- GetDItem(theDialog,EDITSIZE,&t,(Handle *)&h,&r);
- SetIText((Handle)h, fontSize);
-
- resetTogButtons(theDialog,TB1);
- break;
- case SETBTN:
- setNewFont(theDialog);
- break;
- default:
- resetTogButtons(theDialog,TB1);
- break;
- }
- }
- DisposDialog(theDialog);
- }
- }
- //----------------------------------------------------------------------------------
- // A simple dialog filter proc - all it does is limit typing to numbers in the
- // EDITSPIN or EDITSIZE edit text items.
- //----------------------------------------------------------------------------------
- static pascal char filter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
- {
- char result = FALSE;
- char c;
- short itemEdit, t;
- ControlHandle h;
- Rect r;
-
- switch(theEvent->what) {
- case nullEvent: // update the "progress" bar
- GetDItem(theDialog, PROGBAR, &t, (Handle *)&h, &r);
- if((*h)->contrlValue >= (*h)->contrlMax)
- SetCtlValue(h, 0);
- else
- SetCtlValue(h, GetCtlValue(h)+1);
- break;
- case keyDown:
- case autoKey:
- itemEdit = ((DialogPeek) theDialog)->editField + 1;
-
- if(itemEdit == EDITSPIN || itemEdit == EDITSIZE) {
- result = TRUE;
- c = theEvent->message & charCodeMask;
- switch (c) {
- case _BACK: // backspace
- case _TAB: // tab
- case _PLUS: // +
- case _MINUS: // -
- case _LEFT: // left/right arrow keys
- case _RIGHT:
- result = FALSE; // are allowed
- break;
- }
- if(result && (c < _ZERO || c > _NINE)) { // only allow digits
- SysBeep(1);
- }
- else
- result = FALSE;
- }
- break;
- }
- return result;
- }
-
- //----------------------------------------------------------------------------------
- // Set a new font for this dialog
- //----------------------------------------------------------------------------------
- static void setNewFont(DialogPtr theDialog)
- {
- short t,val,newFont;
- long newSize;
- ControlHandle h;
- Rect r;
- Str255 fontName,fontSize;
- FontInfo fInfo;
- popUpPrivateDataH hPpd;
-
- // get the font size from the popup menu
-
- GetDItem(theDialog,FONTSIZE,&t,(Handle *)&h,&r);
- val = GetCtlValue(h);
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
- GetItem((*hPpd)->mHandle, val, fontSize);
- StringToNum(fontSize, &newSize);
-
- // get the font from the popup menu
-
- GetDItem(theDialog,FONTPOPUP,&t,(Handle *)&h,&r);
- val = GetCtlValue(h);
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
- GetItem((*hPpd)->mHandle, val, fontName);
- GetFNum(fontName, &newFont);
-
- // set new font & size for current port
-
- TextFont(newFont);
- TextSize((short)newSize);
-
- // set new font for TEHandle in dialog (this is a crude hack!)
-
- GetFontInfo(&fInfo);
- (*((DialogPeek)theDialog)->textH)->txFont = newFont;
- (*((DialogPeek)theDialog)->textH)->txSize = (short)newSize;
- (*((DialogPeek)theDialog)->textH)->fontAscent = fInfo.ascent;
-
- // redraw the dialog in the new font & size
-
- DrawDialog(theDialog);
- }
-
- //----------------------------------------------------------------------------------
- // Set a new size into the popup menu for font size. Called when the
- // EDITSIZE dialog item is changed, will add or remove a "custom" size
- // and a separator line as required.
- //----------------------------------------------------------------------------------
- static void setNewSize (DialogPtr theDialog, long newSize)
- {
- short type,val,inx,max;
- long longVal;
- ControlHandle h;
- MenuHandle hMenu;
- Rect r;
- popUpPrivateDataH hPpd;
- Str255 s;
- Boolean found=false;
-
- GetDItem(theDialog,FONTSIZE,&type,(Handle *)&h,&r); // get menu handle
- hPpd = (popUpPrivateDataH)(*h)->contrlData;
- hMenu = (*hPpd)->mHandle;
-
- val = GetCtlValue(h); // uncheck current item
- CheckItem(hMenu, val, FALSE);
-
- max = CountMItems(hMenu);
- for(inx=1;inx<=max;inx++) { // see if typed in value
- GetItem(hMenu, inx, s); // matches a menu item
- StringToNum(s, &longVal);
- if(longVal == newSize) { // yep, got a match but it is
- if(inx > 2 & max > MAXFONTSIZES) { // not custom, so let's
- DelMenuItem(hMenu,1); // delete the custom setting
- DelMenuItem(hMenu,1); // and the separator line
- inx-=2;
- }
- CheckItem(hMenu, inx, TRUE); // check new item
- SetCtlValue(h, inx);
- found = true;
- break;
- }
- }
- if(!found) { // no match, set custom item
- NumToString(newSize, s);
- if(max == MAXFONTSIZES) { // add a custom value
- InsMenuItem(hMenu, "\p(-", 0);
- InsMenuItem(hMenu, s, 0);
- }
- else {
- SetItem(hMenu, 1, s); // replace existing custom
- }
- CheckItem(hMenu, 1, TRUE);
- SetCtlValue(h, 1);
- }
- }